﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Documents;

namespace WPFConsole
{
    public partial class PConsole : Window
    {

        void ConnectCommand()
        {
            string input = tbInput.Text;
            List<string> tokens = input.Split(' ').ToList<string>();
            IPEndPoint localEndPoint;
            if (!Connected)
            {
                try
                {
                    sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    localEndPoint = new IPEndPoint(Dns.GetHostAddresses(tokens[1])[Dns.GetHostAddresses(tokens[1]).Length - 1], 1234);
                    sck.Connect(localEndPoint);
                    Connected = true;
                    this.ClearInput();
                    Listener = new Thread(new ThreadStart(Listen));
                    Listener.Start();
                }
                catch (ArgumentOutOfRangeException)
                {
                    Connected = false;
                    this.WriteLine("Insufficient parameters. Usage: connect <IPAddress>");
                }
                catch (Exception e)
                {
                    Connected = false;
                    this.WriteLine("Error: " + e.Message + "\nSource: " + e.Source);
                }
            }
            else
            {
                this.WriteLine("Already connected to a server.");
            }
        }

        void HelpCommand()
        {
            string input = tbInput.Text;
            List<string> tokens = input.Split(' ').ToList<string>();
            string CommandList = "";
            foreach (string command in Commands.Keys)
            {
                CommandList += (command + '\t');
            }
            this.WriteLine("Available client commands: "+CommandList.Trim());
            if (this.Connected)
            {
                this.SendToServer(input);
            }
            this.ClearInput();
        }

        void DisconnectCommand()
        {
            string input = tbInput.Text;
            List<string> tokens = input.Split(' ').ToList<string>();
            this.WriteLine("Will now disconnect from server...");
            this.sck.Close();
            this.Connected = false;
            this.WriteLine("Socket closed. Successfully disconnected from server.");
            this.ClearInput();
        }

        void PingCommand()
        {
            if (Connected)
            {
                SendToServer("PING");
                SendToServer("PING");
                SendToServer("PING");
                SendToServer("PING");
                SendToServer("PING");
                SendToServer("PING");
            }
        }

        void ClearCommand()
        {
            
            if (tbConsole.Dispatcher.Thread == Thread.CurrentThread)
            {
                this.tbConsole.Document.Blocks.Clear();
            }
            else
            {
                tbConsole.Dispatcher.Invoke(new VoidMethod(ClearCommand));
            }
            this.ClearInput();
        }

    }
}
